﻿#light

module DictionaryProcessor

open System
open System.IO
open System.Collections.Generic

let generateFile (dictionary_source : String, writer : StreamWriter, size: int64, rand : Random) = 

    if not (File.Exists(dictionary_source)) then 
        writer.Close()
        raise (new Exception("Incorrect path to dictionary"))       // return to the main program if dictionary file is not exist
        
    // subsidiary objects    
    let dictionary_file = new FileStream(dictionary_source, FileMode.Open, FileAccess.Read, FileShare.Read)                          
    let reader = new StreamReader(dictionary_file)
    let dictionary = new List<String>()
 
    // getting the dictionary values
    while not reader.EndOfStream do
        let word = reader.ReadLine()
        dictionary.Add(word)

    // current data values
    let dictionary_array = dictionary.ToArray()
    let dictionary_size = dictionary.Count     
    let mutable dictionary_count = (int64) 0

    // writing the random words
    while dictionary_count < size do
        let rnd = (rand.Next(dictionary_size) + rand.Next(dictionary_size) + rand.Next(dictionary_size) + rand.Next(dictionary_size) ) / 4
        let word = dictionary_array.[rnd]
        writer.Write(word + " ")
        dictionary_count <- dictionary_count + (int64) word.Length + (int64) 1